<?php
declare (strict_types=1);

namespace app\listener;

use app\constant\CommonConstant;
use app\event\FacebookPixelTrackEvent;
use app\exception\PushShopifyOrderException;
use app\exception\QueueRuntimeException;
use app\model\LineItems;
use app\model\Notify;
use app\model\Orders;
use app\model\PixelLogs;
use app\model\ShopsPayment;
use app\service\pixel\FaceBookPixelTrack;
use app\service\shopify\action\graph\OrderGraph;
use app\service\shopify\action\rest\DraftOrderRest;
use app\service\shopify\action\rest\OrderRest;
use app\trait\ShopifyTrait;
use think\helper\Arr;

class FacebookPixelTrackListener
{

    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle(FacebookPixelTrackEvent $event)
    {
        $order = $event->getOrder();
        try {
          if($order->traffic_platform == CommonConstant::TRAFFIC_SOURCE_TK) return false;
          $products = [];
          $item = $order->items()->findOrEmpty();
          $products = [
              "product_title"=>$item->title,
              "quantity"=>$item->quantity,
          ];


          $params = [
              "ip"=>$order->ip,
              "order"=>[
                  "id"=>pathinfo($order->draft_id,PATHINFO_BASENAME),
                  "amount"=>$order->total_present,
                  "currencyCode"=>$order->present_currency,
              ],
              "address"=>[
                  "zip"=>$order->shippingAddress->zip,
                  "city"=>$order->shippingAddress->city,
                  "email"=>$order->email,
                  "phone"=>$order->shippingAddress->phone,
                  "state"=>$order->shippingAddress->province,
                  "last_name"=>$order->shippingAddress->last_name,
                  "first_name"=>$order->shippingAddress->first_name,
                  "country_code"=>$order->shippingAddress->country_code,
              ],
              "website"=>$order->website,
              "shop_code"=>$order->shop->name,
              "event_type"=>CommonConstant::FB_EVENT_PURCHASE,
              "user_agent"=>$order->user_agent,
              "products"=>Arr::first($products)
          ];

            $data = [
                'ip' => $order->ip,//获取用户IP地址
                'event_type' => CommonConstant::FB_EVENT_PURCHASE,
                'fbclid' => $order->fbclid,
                'user_agent' => $order->user_agent,
                'website' => $order->website,
                'params' => $params,
                "fbc_at" => $order->fbc_at,
                'response' => [],
                'product_id' => 0,
                'order_id' => Arr::get($params,'order.id'),
                'platform' => $order->traffic_platform,
                'shop_code' => Arr::get($params,'shop_code')
            ];
            $pixelLog = PixelLogs::createTrackLog($data);
            if(!$pixelLog->isEmpty()){
                $service = new FaceBookPixelTrack();
                $service->Track_purchase($pixelLog);
            }
        } catch (\Exception $e) {
            Orders::saveExceptionMessage($order, $e->getMessage());
        }
    }

}
